package com.example.communist_app_api.service.impl;

import com.example.communist_app_api.db.dao.RolePermissionDao;
import com.example.communist_app_api.service.RolePermissionService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author love~mlm
 * @version 1.0
 * Create by 2022/11/19 15:36
 */
@Transactional
@Service
public class RolePermissionServiceImpl implements RolePermissionService {

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    public ArrayList<Integer> getPermissionIdByRoleId(Integer id) {
        return rolePermissionDao.getPermissionIdByRoleId(id);
    }

    @Transactional
    @Override
    public Boolean updateRoleAssignPermission(List<Integer> permissionIdList,Integer roleId) {
        //删除之前role角色的选项
        rolePermissionDao.deleteAllPermissionByRoleId(roleId);
        //循环遍历前台勾选的选项
        for (Integer i : permissionIdList) {
            Integer insert = rolePermissionDao.updateRoleAssignPermission(i,roleId);
        }
        return true;
    }

    @Override
    public Integer updateRolePermissionOut(Integer permissionId) {
        return rolePermissionDao.deleteByPermissionId(permissionId);
    }
}
